package math;

import com.hspedu.collection_.Book;

import java.util.Arrays;
import java.util.Comparator;

public class ArrayExercise {
    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("红楼梦",100);
        books[1] = new Book("金瓶梅",90);
        books[2] = new Book("青年文摘",5);
        books[3] = new Book("java从入门到放弃",300);

        Arrays.sort(books,new Comparator(){
            public int compare(Object o1, Object o2){
                Book book1 = (Book) o1;
                Book book2 = (Book) o2;
                double result = book1.getName().length() - book2.getName().length();
                if(result > 0){
                    return -1;
                }else if(result < 0){
                    return 1;
                }else {
                    return 0;
                }
            }
        });

        for (int i = 0; i < books.length; i++) {
            System.out.println(books[i]);
        }
        Book[] books1 = new Book[4];
    }

    public static void bubble(Book[] books, Comparator c){
        for (int i = 0; i < books.length - 1; i++) {
            for (int j = 0; j < books.length - 1 - i; j++) {
                if(c.compare(books[j],books[j + 1]) < 0){
                    Book temp = books[j];
                    books[j] = books[j + 1];
                    books[j + 1] = temp;
                }
            }
        }
    }


}
